{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组\n",
    "* 元组与列表类似，不同之处在于元组的元素不能修改。\n",
    "* 元组使用小括号，列表使用方括号。\n",
    "* 元组创建很简单，只需要在括号中添加元素，并使用逗号隔开即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 'hello', True)\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tup1=(1,2,'hello',True)\n",
    "print(tup1)\n",
    "print(type(tup1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 'hello', True)\n"
     ]
    }
   ],
   "source": [
    "tup1=1,2,'hello',True\n",
    "print(tup1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "()\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tup2=()\n",
    "print(tup2)\n",
    "tup2=(1,)\n",
    "print(type(tup2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 访问元组\n",
    "* 元组可以使用下标索引来访问元组中的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 'hello', True)\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "print(tup1)\n",
    "print(tup1[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 修改元组\n",
    "* 元组中的元素值是不允许修改的，但我们可以对元组进行连接组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 'hello') (1, 3, 'world')\n",
      "(1, 2, 'hello', 1, 3, 'world')\n"
     ]
    }
   ],
   "source": [
    "tup1=1,2,'hello'\n",
    "tup2=1,3,'world'\n",
    "print(tup1,tup2)\n",
    "print(tup1+tup2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 删除元组\n",
    "* 元组中的元素值是不允许删除的，但我们可以使用del语句来删除整个元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2)\n"
     ]
    }
   ],
   "source": [
    "tup2=(1,2)\n",
    "print(tup2)\n",
    "del tup2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组运算符\n",
    "* 与字符串一样，元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制，运算后会生成一个新的元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 'hello', 1, 3, 'world', 1, 2, 'hello', 1, 3, 'world')\n",
      "(1, 2, 'hello', 1, 3, 'world', 1, 2, 'hello', 1, 3, 'world')\n"
     ]
    }
   ],
   "source": [
    "tup1=(1, 2, 'hello', 1, 3, 'world')\n",
    "print(tup1+tup1)\n",
    "print(tup1*2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "print(len(tup1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print('helo' in tup1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 \n",
      "\n",
      "2 \n",
      "\n",
      "hello \n",
      "\n",
      "1 \n",
      "\n",
      "3 \n",
      "\n",
      "world \n",
      "\n"
     ]
    }
   ],
   "source": [
    "for x in tup1:print(x,'\\n')    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组索引，截取\n",
    "* 因为元组也是一个序列，所以我们可以访问元组中的指定位置的元素，也可以截取索引中的一段元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(2, 'hello')\n"
     ]
    }
   ],
   "source": [
    "tup1=(1, 2, 'hello', 1, 3, 'world')\n",
    "print(tup1[1])\n",
    "print(tup1[1:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组内置函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "9\n",
      "1\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tup1=(1,2,3,5,7,9)\n",
    "# len\n",
    "print(len(tup1))\n",
    "# max\n",
    "print(max(tup1))\n",
    "# min\n",
    "print(min(tup1))\n",
    "lst1=[1,2,3]\n",
    "print(type(tuple(lst1)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
